package com.yeren.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ActionServlet extends HttpServlet{
	public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
			PrintWriter out=response.getWriter();
			//获取请求资源路径
			String uri=request.getRequestURI();
			String path=uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
			if(path.equals("/get_text")){
				out.println("来自星星的你");
			}else if(path.equals("/post_text")){
				String name=request.getParameter("uname");
				System.out.println(name);
				out.println("又来了一次的"+name);
			}else if(path.equals("/check_name")){
				String name=request.getParameter("uname");
				//模拟一个耗时的操作，网络延时
				if(1==1){
					try{
						Thread.sleep(4000);
					}catch(InterruptedException e){
						e.printStackTrace();
					}
				}
				System.out.println(name);
				if("luffy".equals(name)){
					System.out.println("==");
					out.println("该用户名存在");
				}else{
					System.out.println("++");
					out.println("可以使用");
				}
			}else if(path.equals("/getCity")){
				String name=request.getParameter("name");
				if("bj".equals(name)){
					out.println("朝阳,cy;东城,dc");
				}else if("sh".equals(name)){
					out.println("静安,ja;黄埔,hp;浦东新,pdx");
				}else if("gz".equals(name)){
					out.println("白云,by;番禺,py");
				}
			}
		out.close();
	}
}
